﻿<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class tochuyenmon_Model extends Model {

    function __construct(){
        parent::__construct();
    }
    public function SELECT_TO_CHUYEN_MON_ALL_TR($arr_filter){
        $P_SITE_ROOT = SITE_ROOT;
        $P_MA_DON_VI = $arr_filter['cbx_MA_DON_VI_filter'];
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_TO_CHUYEN_MON_ALL_TR
                                                      (:CUR,
                                                      '$P_MA_DON_VI',
                                                      '$P_SITE_ROOT'
                                                      );END;",'CUR');
    }
    public function SELECT_TO_CHUYEN_MON_ALL_TR2($P_MA_DON_VI){
        $P_SITE_ROOT = SITE_ROOT;
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_TO_CHUYEN_MON_ALL_TR
                                                      (:CUR,
                                                      '$P_MA_DON_VI',
                                                      '$P_SITE_ROOT'
                                                      );END;",'CUR');
    }
    public function qry_all_tochuyenmon(){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_TO_CHUYEN_MON(:CUR); END;",'CUR');
    }
    public function qry_all_tochuyenmon_filter($arr_filter)
    {
        $P_MA_DON_VI = $arr_filter['cbx_MA_DON_VI_filter'];
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_TO_CHUYEN_MON_FILTER
                                                      (:CUR,
                                                      '$P_MA_DON_VI'
                                                      );END;",'CUR');
    }
    public function qry_single_donvi($P_MA_DON_VI){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_DON_VI_DETAIL(:CUR,'$P_MA_DON_VI'); END;",'CUR');
    }
    public function qry_all_tochuyenmon_by_mdv($P_MA_DON_VI){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_TO_CHUYEN_MON_BY_MDV(:CUR,'$P_MA_DON_VI'); END;",'CUR');
    }
    public function qry_single_tochuyenmon($MA_TO_CHUYEN_MON){
        return $this->db->ExecuteCursor("BEGIN QLCB.SELECT_TO_CHUYEN_MON_DETAIL(:CUR,'$MA_TO_CHUYEN_MON'); END;",'CUR');
    }
    public function update_tochuyenmon(){
        $P_UPDATE_TYPE = get_post_var('hdn_update_type');
        if($P_UPDATE_TYPE=='CAP_NHAT'){
            $P_MA_TO_CHUYEN_MON = get_post_var('txt_MA_TO_CHUYEN_MON');
        } else{
            $P_MA_TO_CHUYEN_MON = 0;
        }
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_TEN_TO_CHUYEN_MON = get_post_var('txt_TEN_TO_CHUYEN_MON');
        $P_MA_DON_VI = get_post_var('cbx_MA_DON_VI');
        $P_STT_TO_CHUYEN_MON = get_post_var('txt_STT_TO_CHUYEN_MON');
        $P_TRANG_THAI = get_post_var('cbx_TRANG_THAI');

        $stmt = $this->db->PrepareSP("BEGIN QLCB.UPDATE_TO_CHUYEN_MON(:P_VAL,:P_UPDATE_TYPE,:P_NGUOI_CAP_NHAT,:P_MA_TO_CHUYEN_MON,:P_TEN_TO_CHUYEN_MON,:P_MA_DON_VI,:P_STT_TO_CHUYEN_MON,:P_TRANG_THAI);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_UPDATE_TYPE,'P_UPDATE_TYPE');
        $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
        $this->db->InParameter($stmt,$P_MA_TO_CHUYEN_MON,'P_MA_TO_CHUYEN_MON');
        $this->db->InParameter($stmt,$P_TEN_TO_CHUYEN_MON,'P_TEN_TO_CHUYEN_MON');
        $this->db->InParameter($stmt,$P_MA_DON_VI,'P_MA_DON_VI');
        $this->db->InParameter($stmt,$P_STT_TO_CHUYEN_MON,'P_STT_TO_CHUYEN_MON');
        $this->db->InParameter($stmt,$P_TRANG_THAI,'P_TRANG_THAI');
        $chaythutuc = $this->db->Execute($stmt);
        //echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->popup_exec_done();
          }elseif($P_VAL == 'TON_TAI_MA'){
              $this->popup_exec_fail(__('Mã tổ chuyên môn đã tồn tại!'));
              return;
          }elseif($P_VAL == 'TON_TAI_TEN'){
              $this->popup_exec_fail(__('Tên tổ chuyên môn đã tồn tại!'));
              return;
          }else{
               $this->popup_exec_fail(__('Không thể cập nhật!'));
               return;
          }
        }
    }
    public function delete_tochuyenmon(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_TO_CHUYEN_MON = get_post_var('hdn_item_id',0);
        //echo '<script>alert("'.$P_MA_TO_CHUYEN_MON.'");</script>';
        $stmt = $this->db->PrepareSP("BEGIN QLCB.DELETE_TO_CHUYEN_MON(:P_VAL,:P_NGUOI_XOA,:P_MA_TO_CHUYEN_MON);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_MA_TO_CHUYEN_MON,'P_MA_TO_CHUYEN_MON');
        $chaytochuyenmon = $this->db->Execute($stmt);
        if ($chaytochuyenmon){
          if ($P_VAL == 'THANH_CONG'){
              $this->exec_done($this->goback_url);
          }else{
               $this->popup_exec_fail(__('Không thể xóa!'));
               return;
          }
        }
    }

}